package com.hsy.crm.web.web.basics;

import com.hsy.crm.web.domain.cm.Response;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Store;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.vo.store.StonePageVo;
import com.hsy.crm.web.domain.vo.store.StoreSelectVo;
import com.hsy.crm.web.service.impl.StoneService;
import com.hsy.crm.web.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/store")
public class StoreAction extends BaseAction {

    @Autowired
    StoneService stoneService;

    @RequestMapping("/insert")
    public String insert(Store store) {
        String response = Response.body(Unified.newInstance(stoneService.insert(store,Boolean.TRUE)));
        return response;
    }

    @RequestMapping("/setDefault")
    public String setDefault(Store store) {
        String response = Response.body(stoneService.setDefault(store));
        return response;
    }

    @RequestMapping("/update")
    public String update(Store store) {
        String response = Response.body(Unified.newInstance(stoneService.update(store,Boolean.TRUE)));
        return response;
    }

    @RequestMapping("/delete")
    public String delete(Store store) {
        //check
        if( store.getDefaultStone() == null ){
            throw new ServiceException("store defualtStore error");
        }

        //buss
        Unified<Integer> unified = stoneService.deleteStore(store);
        String response =  Response.body(unified);
        return response;
    }

    @RequestMapping("/select")
    public String select(StoreSelectVo store) {

        if( store.getCompanyId()==null ){
            store.setCompanyId(store.getLoginCompanyId());
        }

        if( store.getTransfer() != null && store.getTransfer() == 1 ){
            store.setCompanyId(null);
        }

        return Response.body(stoneService.select(store));
    }

    @RequestMapping("/get")
    public String get(Store store) {
        return Response.body(Unified.newInstance(stoneService.selectOne(store)));
    }

    @RequestMapping("/getDefault")
    public String getDefault(Store store) {
        return Response.body(stoneService.getDefault(store));
    }

    @RequestMapping("/page")
    public String page(StonePageVo stonePageVo) {
        return Response.body(stoneService.page(stonePageVo));
    }

}
